#!/usr/bin/perl -w
######################################################
# file : perl/diarytagcloud.pl
# Author : Anil Kainikara (anilkumar911@gmail.com)
# Print all tags in the diary
#
# This code is licenced under the GPL.
#
######################################################
use Time::Local 'timelocal_nocheck';
#use Getopt::Std;
use Getopt::Long;
use DBI;
use strict;

my @weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
my @searchwords;
my @excludewords;

my @patterns;
my @tags;
my $showfullsearch=0;

my $db = DBI->connect("dbi:SQLite:/home/anil/.local/share/diary.db", "", "",
{RaiseError => 1, AutoCommit => 1});

sub main
{
  parsesearchargs();
  search();
}
   

sub usage()
{
   print "todo : @_"
}

sub parsesearchargs
{
  Getopt::Long::Configure ('bundling');
  GetOptions('t=s'=> \@tags);
}

sub search()
{
  my $sth = $db->prepare("SELECT day, month, year, content  FROM  entries");

  my $rv = $sth->execute();
  my $day=0;
  my $month=0;
  my $year=0;
  my $content="";

  my $searchpattern="";
  my $pattern="";

  my @tags;

  $searchpattern = "\<[a-z0-9]+ *:{0,1} *[a-z 0-9]*\>|\[[a-z0-9]+\]";
  print "search pattern is $searchpattern \n";
  while ( ($day,$month,$year,$content) =  $sth->fetchrow_array() )
  {
    my $day_of_week = get_day($month."-".$day."-".$year);
    #print $content;

    my $dateprinted=0;
    my $block="";
    while ( $content =~ m/($searchpattern)/sig )
    {
       print "$1 \n";
       #$tags[$1]++;
    }
  }
}


sub get_day {
my $date = shift || return(0);
my ($mon,$mday,$year) = $date =~ /(\d+)-(\d+)-(\d+)/;
my $epochtime = timelocal_nocheck(0, 0, 0, $mday, $mon-1, $year);
my $day = (localtime($epochtime))[6];
return $weekday[$day];
}

main(@ARGV);
